﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using OLTS.DATA_SERVICE;
using OLTS.MODEL;
using OLTS.WEB.BaseClass;

namespace ONTS.WEB.Service
{
    /// <summary>
    /// Summary description for WorkDetailService
    /// </summary>
    public class WorkDetailService : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            var model = new OLTS.MODEL.OTS_TIME_SHEET();
            var child = new OLTS.MODEL.OTS_WORK_DETAIL();
            var currentuser = BaseService.LOGIN.EMPLOYEE_ID;

            DataSource.RemoveAll(x => x.Action != ActionEnum.Create || x.Action != ActionEnum.Update);
            var date = request.QueryString["date"].ToString();
            var cmd = new WorkDetail_Service();
            var result = cmd.GetByCurrentUserandDate(currentuser, Convert.ToDateTime(date));    
            var row = 0;

            foreach (var ch in result)
            {
                row = row + 1;
                var obj = new OLTS.MODEL.OTS_WORK_DETAIL();
                obj.ID = row;
                obj.DETAIL = ch.DETAIL;
                obj.AMOUNT_OF_TIME = ch.AMOUNT_OF_TIME;
                obj.AMOUNT_OF_TIME_OVER_TIME = ch.AMOUNT_OF_TIME_OVER_TIME;
                obj.JOB_CODE = ch.JOB_CODE;
                obj.REMARK = ch.REMARK;
                DataSource.Add(obj);
            }

            if (request["oper"] == "add")
            {
                var rowix = DataSource.Count;
                child.ID = rowix + 1;
                child.SEQ = rowix + 1;
                if (request["AMOUNT_OF_TIME"] != string.Empty)
                {
                    child.AMOUNT_OF_TIME = Utility.CheckInt(request["AMOUNT_OF_TIME"]);
                }

                if (request["AMOUNT_OF_TIME_OVER_TIME"] != string.Empty)
                {
                    child.AMOUNT_OF_TIME_OVER_TIME = Utility.CheckInt(request["AMOUNT_OF_TIME_OVER_TIME"]);
                }

                child.DETAIL = request["DETAIL"];
                child.CREATED = DateTime.Now;
                child.CREATE_BY = BaseService.LOGIN.EMPLOYEE_ID;
                child.Action = OLTS.MODEL.ActionEnum.Create;
                DataSource.Add(child);

            }
            else
            {
                if (request["oper"] == "del")
                {
                    var rowix = Convert.ToInt32(request["ID"]);
                    var modeldel = DataSource.Where(x => x.ID == rowix).FirstOrDefault();
                    if (modeldel != null)
                    {
                        DataSource.Remove(modeldel);
                        // modeldel.Action = OLTS.MODEL.ActionEnum.Delete;
                    }
                }
                else if (request["oper"] == "edit")
                {
                    var rowix = Convert.ToInt32(request["ID"]);
                    var modeledit = DataSource.Where(x => x.ID == rowix).FirstOrDefault();
                    if (modeledit != null)
                    {
                        if (request["AMOUNT_OF_TIME"] != string.Empty)
                        {
                            modeledit.AMOUNT_OF_TIME = Utility.CheckInt(request["AMOUNT_OF_TIME"]);
                        }
                        if (request["AMOUNT_OF_TIME_OVER_TIME"] != string.Empty)
                        {
                            modeledit.AMOUNT_OF_TIME_OVER_TIME = Utility.CheckInt(request["AMOUNT_OF_TIME_OVER_TIME"]);
                        }
                        modeledit.DETAIL = request["DETAIL"];
                        modeledit.Action = ActionEnum.Update;
                    }
                }
            }
            
            DataSource = DataSource.OrderBy(o => o.ID).ToList();

            int total = DataSource.Count == 0 ? 1 : (int)Math.Ceiling((double)DataSource.Count / 10);

            var jsonData = new
            {
                total = total,
                page = request["page"],
                records = DataSource.Count,
                rows = DataSource.Skip(Convert.ToInt16(request["page"]) * 10 - 10).Take(10)
            };

            response.Write(new JavaScriptSerializer().Serialize(jsonData));
        }

        public List<OLTS.MODEL.OTS_WORK_DETAIL> DataSource
        {
            get
            {
                var d = (List<OLTS.MODEL.OTS_WORK_DETAIL>)HttpContext.Current.Session["detail"];
                if (d == null)
                {
                    d = new List<OLTS.MODEL.OTS_WORK_DETAIL>();
                    HttpContext.Current.Session["detail"] = d;
                }
                return d;
            }
            set
            {
                HttpContext.Current.Session["detail"] = value;
            }
        }

        public void clearDatasource()
        {
            
        }
        
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}